<template>
  <div class="home-category" @mouseleave="categoryId = null">
    <!-- 左侧分类栏 -->
    <ul class="menu">
      <li :class="{active: categoryId === items.id}" v-for="items in menuList" :key="items.id" @mouseenter="categoryId = items.id">
        <router-link to="/">{{ items.name }}</router-link>
        <template v-if="items.children">
          <router-link to="/" v-for="item in items.children" :key="item.id">{{ item.name }}</router-link>
        </template>

        <!-- 白屏缓冲骨架 -->
        <template v-else>
          <xtxSkeleton width="60px"  height="18px" bg="rgba(255,255,255,0.2)" :animated="true" style="margin-right: 5px"/>
          <xtxSkeleton width="60px" height="18px" bg="rgba(255,255,255,0.2)" :animated="true"/>
        </template>
      </li>
    </ul>

    <div class="layer">
      <h4>分类推荐<small>根据你的购买记录或浏览记录推荐</small></h4>
      <!-- 分类推荐 -->
      <ul class="layer-class" v-if="cunCategory && cunCategory.goods">
        <li v-for="item in cunCategory.goods" :key="item.id">
          <router-link to="/">
            <img :src="item.picture" alt="">
            <div class="info">
              <p class="name ellipsis-2">{{ item.name }}</p>
              <p class="desc ellipsis">{{ item.desc }}</p>
              <p class="price"><i>￥</i>{{ item.price }}</p>
            </div>
          </router-link>
        </li>
      </ul>

      <!-- 品牌推荐 -->
      <ul class="layer-class" v-if="cunCategory && cunCategory.brands">
         <li class="brand" v-for="item in cunCategory.brands" :key="item.id">
          <router-link to="/">
            <img :src="item.picture" alt="">
            <div class="info">
              <p class="place"><i class="iconfont icon-dingwei"></i>{{ item.place }}</p>
              <p class="name ellipsis">{{item.name}}</p>
              <p class="desc ellipsis-2">{{ item.desc }}</p>
            </div>
          </router-link>
         </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { computed, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { findBrand } from '@/api/home'
export default {
  name: 'HomeCategory',
  components: {
  },
  // 获取1级分类，并且只需要两个二级分类
  // 需要在组件内部 定义一个品牌数据
  // 根据vuex的分类数据和组件中定义品牌数据，得到左侧分类完整数据，9个分类
  setup () {
    // 新创建一个分类
    const brand = reactive({
      id: 'brand',
      name: '品牌',
      children: [
        {
          id: 'brand-children',
          name: '品牌推荐'
        }
      ],
      brands: [] // 品牌数据
    })

    const store = useStore()
    const menuList = computed(() => {
      // 修改每个分类的二级分类 只截取前两个二级分类
      const list = store.state.category.list.map(item => {
        return {
          id: item.id,
          name: item.name,
          children: item.children && item.children.slice(0, 2),
          goods: item.goods
        }
      })

      // 将新创建的分类添加到数组中
      list.push(brand)
      return list
    })

    const categoryId = ref(null)
    const cunCategory = computed(() => {
      return menuList.value.find(item => {
        return item.id === categoryId.value
      })
    })
    console.log(cunCategory)

    findBrand().then(data => {
      brand.brands = data.result
      console.log(data)
    })

    return { menuList, cunCategory, categoryId }
  }

}
</script>

<style lang="less" scoped>
.home-category {
  width: 250px;
  height: 500px;
  background: rgba(0, 0, 0, 0.8);
  position: relative;
  z-index: 99;
  .menu {
    li {
      padding-left: 40px;
      height: 50px;
      line-height: 50px;
      &:hover,&.active { // 当选中的分类ID与当前分类ID相同时，则添加active类名
        background: @xtxColor;
      }
      a {
        margin-right: 4px;
        color: #fff;
        &:first-child{
          font-size: 16px;
        }
      }
    }
  }
  .layer {
    width: 990px;
    height: 500px;
    background: rgba(255, 255, 255, 0.8);
    position: absolute;
    left: 250px;
    top: 0;
    padding: 0 15px;
    display: none;
    h4 {
      font-size: 20px;
      font-weight: normal;
      line-height: 80px;
      small {
        font-size: 16px;
        color: #666;
      }
    }
    .layer-class {
      display: flex;
      flex-wrap: wrap;
      li {
        width: 310px;
        height: 120px;
        margin-right: 15px;
        margin-bottom: 15px;
        border-radius: 4px;
        border: 1px solid #eee;
        background: #fff;
        &:nth-child(3n) {
          margin-right: 0;
        }
        a {
          display: flex;
          width: 100%;
          height: 100%;
          align-items: center;
          padding: 10px;
          &:hover {
            background: #e3f9f4;
          }
          img {
            width: 95px;
            height: 95px;
            margin-right: 10px;
          }
          .info {
            line-height: 24px;
            width: 190px;
            .name {
              font-size: 16px;
              color: #666;
            }
            .desc {
              color: #999;
            }
            .price {
              font-size: 22px;
              color: @priceColor;
              i{
                font-size: 16px;
              }
            }
          }
        }
      }
      li.brand{
        height: 180px;
        a {
          align-items: flex-start;
          img {
            width: 120px;
            height: 160px;
          }
          .info {
            p {
              margin-top: 8px;
            }
            .place {
              color: #999;
            }
          }
        }
      }
    }

  }
  &:hover {
    .layer {
      display: block;
    }
  }
}
.xtx-skeleton {
  animation: fade 1s linear infinite alternate;
}
  @keyframes fade {
      from {
        opacity: 0.3;
      }
      to {
        opacity: 1;
      }
    }
</style>
